package site.service;

import java.util.List;

import site.entity.Article;
import site.entity.Category;
import site.entity.Role;
import site.entity.User;

public class SiteSystem {

	/**
	 * 初始化网站系统
	 */
	public static void initiate() {
		System.out.println("初始化系统...");

		UserRepository userRepository = new UserRepository();

		// ===== 创建基本角色 ===== //
		Role role = new Role();
		role.setName("系统超级管理员");
		userRepository.saveRole(role);
		role.setName("网站管理员");
		userRepository.saveRole(role);

		// ===== 创建基本用户 ===== //
		User user = new User();
		user.setName("SiteAdmin");
		user.setEmail("neolimeng@gmail.com");
		user.setPassword("neowoxnsk");
		user.setRid(Role.ADMIN);
		user.setStatus(User.STATUS_ACTIVATED);
		userRepository.saveUser(user);
		user = userRepository.getUserByUuid(user.getUuid());
		user = new User();
		user.setName("管理员");
		user.setEmail("sjjx@ustb.edu.cn");
		user.setPassword("sjjxsjjx");
		user.setRid(Role.REVIEWER);
		user.setStatus(User.STATUS_ACTIVATED);
		userRepository.saveUser(user);
		user = userRepository.getUserByUuid(user.getUuid());
		User user2 = new User();
		user2.setName("用户");
		user2.setEmail("sjjxuser@ustb.edu.cn");
		user2.setPassword("sjjxsjjx");
		user2.setRid(Role.INPUT);
		user2.setStatus(User.STATUS_ACTIVATED);
		userRepository.saveUser(user2);

		// ===== 创建类别基本信息 ===== //
		CategoryRepository categoryRepository = new CategoryRepository();
		String[] categories = { "网站相关", "平台简介", "重要论述", "理论探索", "教学资源", "成果展示",
				"基地建设", "实践联盟", "在线调查" };
		for (int i = 0; i < categories.length; i++) {
			Category category = new Category(i + 81, categories[i]);
			category.setParentCid(81 + i);
			categoryRepository.saveCategory(category);
		}

		// 添加子栏目
		int cid = 1;
		String[] categories02 = { "平台简介", "机构组成", "工作流程" };
		for (int i = 0; i < categories02.length; i++) {
			cid++;
			Category category = new Category(cid, categories02[i]);
			category.setParentCid(82);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		String[] categories03 = { "文件", "讲话", "会议精神" };
		for (int i = 0; i < categories03.length; i++) {
			cid++;
			Category category = new Category(cid, categories03[i]);
			category.setParentCid(83);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		String[] categories04 = { "著作要览", "论文集萃", "思想纵横" };
		for (int i = 0; i < categories04.length; i++) {
			cid++;
			Category category = new Category(cid, categories04[i]);
			category.setParentCid(84);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		String[] categories05 = { "教学方案", "教学课件", "教学录像", "视频资源" };
		for (int i = 0; i < categories05.length; i++) {
			cid++;
			Category category = new Category(cid, categories05[i]);
			category.setParentCid(85);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		String[] categories06 = { "调研报告", "优秀论文", "DV短片", "其他成果" };
		for (int i = 0; i < categories06.length; i++) {
			cid++;
			Category category = new Category(cid, categories06[i]);
			category.setParentCid(86);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		String[] categories07 = { "基地掠影", "经验之谈" };
		for (int i = 0; i < categories07.length; i++) {
			cid++;
			Category category = new Category(cid, categories07[i]);
			category.setParentCid(87);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		String[] categories08 = { "实践联盟" };
		for (int i = 0; i < categories08.length; i++) {
			cid++;
			Category category = new Category(cid, categories08[i]);
			category.setParentCid(88);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		String[] categories09 = { "在线调查" };
		for (int i = 0; i < categories09.length; i++) {
			cid++;
			Category category = new Category(cid, categories09[i]);
			category.setParentCid(89);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		String[] categories01 = { "通知公告", "最新动态", "实践风采", "实践之星", "资源征集" };
		for (int i = 0; i < categories01.length; i++) {
			cid++;
			Category category = new Category(cid, categories01[i]);
			category.setParentCid(81);
			category.setLevel(2);
			categoryRepository.saveCategory(category);
		}
		// String[] categories10 = { "通知公告" };
		// for (int i = 0; i < categories10.length; i++) {
		// cid++;
		// Category category = new Category(cid, categories10[i]);
		// category.setParentCid(81);
		// categoryRepository.saveCategory(category);
		// }

		// === 创建临时测试文章 === //
		ArticleRepository articleRepository = new ArticleRepository();
		List<Category> articleCategories = new CategoryRepository()
				.getAllSubCategories();

		for (int j = 0; j < articleCategories.size(); j++) {
			Category category = articleCategories.get(j);
			for (int i = 0; i < 25; i++) {
				Article article = new Article();
				article.setTitle(category.getName() + "文章" + (i + 1));
				article.setContent("文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容文章内容");
				article.setAuthor(user);
				article.setCid(category.getCid());
				article.setStatus(Article.STATUS_CHECKED_PASS);
				articleRepository.save(article);
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SiteSystem.initiate();
	}

}
